package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    @RequestMapping("/findAll")
    public Result findAll(Integer id){
        try {
            List<Menu> list = roleService.findAll(id);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,list);
        }catch (Exception e){
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id){
        try {
            Integer[] ids = roleService.findMenuIdsByRoleId(id);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,ids);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
            PageResult page = roleService.findPage(queryPageBean);
           return page;
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Role byId = roleService.findById(id);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,byId);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] menuIds){
        try{
            roleService.edit(role,menuIds);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS);
    }
}
